﻿namespace YunQue.WebApi.Extensions
{
    /// <summary>
    /// swagger扩展
    /// </summary>
    public static class SwaggerExtension
    {
        /// <summary>
        /// 添加Swagger配置
        /// </summary>
        /// <param name="service"></param>
        public static IServiceCollection AddSwaggerConfig(this IServiceCollection service)
        {
            service.AddSwaggerGen(options =>
            {
                SwaggerGroupSetting.GroupList.ForEach(item =>
                {
                    options.SwaggerDoc(item.GroupName, new OpenApiInfo()
                    {
                        Version = item.Version,
                        Title = item.Title,
                        Description = item.Description
                    });
                });
                var xml = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xml));

                options.AddSecurityDefinition("JwtBearer", new OpenApiSecurityScheme()
                {
                    Description = "身份认证",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.Http,
                    Scheme = JwtBearerDefaults.AuthenticationScheme,
                });
                var scheme = new OpenApiSecurityScheme()
                {
                    Reference = new OpenApiReference() { Type = ReferenceType.SecurityScheme, Id = "JwtBearer" }
                };
                options.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    [scheme] = new string[0]
                });
               
            });
            return service;
        }

    }
}
